<?php

namespace App\Models\Repair;

use App\Models\Common\CommonModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class RepairFactory extends CommonModel
{
    use HasFactory;

    protected $table = 'repair_factory';

    public function getComments() {
        return $this->hasMany(ReapirFactoryComment::class,'repair_factory_id','id');
    }

    public function getAdmin() {
        return $this->hasMany(RepairFactoryAdmin::class,'factory_id','id');
    }

    public function getStar() {
        $stars = ReapirFactoryComment::where('repair_factory_id',$this->id)->get(['star']);
        if($stars->count()<=0){
            return env('STAR',3);
        }
        return (int)ceil($stars->sum('star')/$stars->count());
    }
}
